package com.web.auth.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.comm.db.jdbc.JdbcUtil;

import com.web.auth.UserService;
import com.web.auth.entity.SysUserBean;

public class UserServiceImpl implements UserService {

	public SysUserBean getSysUser(String userName, String password) { 
		SysUserBean user=new SysUserBean();
	   String sql=" SELECT id, username,userpasswrod, m_tel, gs_tel, " +
	   " company, zhiwei, remark, dstatus  FROM tg_db.sysur WHERE username=? " +
	   " AND userpasswrod=? "; 
	   List<String> list=new ArrayList<String>(); list.add(userName);
	   list.add(password);
	   List<Map<String, Object>> listMap =   JdbcUtil.getListMap(sql, list);
	   if (listMap.isEmpty()) {
			return user;
		} else {
			try {
				Map dbMap=listMap.get(0);
				if(dbMap==null||dbMap.isEmpty()){
					return user;
				}
				user.setUserId( dbMap.get("id") + "");
				user.setUserName( dbMap.get("username") + "");
				int status = Integer.parseInt(dbMap.get("dstatus")
						+ "");
				user.setTStatus(status);
				user.setM_tel(dbMap.get("m_tel") + "");
				user.setGs_tel(dbMap.get("gs_tel") + "");
				user.setCompanyName(dbMap.get("company") + "" );
				user.setZhiwei(dbMap.get("zhiwei") + "" );
				
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	   return user; 
	}
	public boolean isLoginSuccess(String userName, String password) {
		try {
			if (!check(userName, password)) {
				return false;
			}
			String sql = "  SELECT 	COUNT(id) cnt, dstatus	 FROM tg_db.sysur WHERE  username=? AND userpasswrod= ?  ";
			List<String> list = new ArrayList<String>();
			list.add(userName);
			list.add(password);
			List<Map<String, Object>> listMap = JdbcUtil.getListMap(sql, list);
			if (listMap.isEmpty()) {
				return false;
			} else {
				try {
					Map dbMap=listMap.get(0);
					int cnt = Integer.parseInt(dbMap.get("cnt") + "");
					if(cnt==0){
						return false;
					}
					int status = Integer.parseInt(dbMap.get("dstatus")
							+ "");
					if (cnt > 0 && status > 0) {
						return true;
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	private boolean check(String name, String pw) {
		if (name == null || name.trim().length() == 0) {
			return false;
		}
		if (pw == null || pw.trim().length() == 0) {
			return false;
		}
		return true;
	}



}
